package jj.jjstudy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jj.jjstudy.dao.UserMapper;
import jj.jjstudy.entity.User;
import jj.jjstudy.response.JsonData;
import jj.jjstudy.response.ResponseData;
import jj.jjstudy.service.UserService;
import jj.jjstudy.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl  implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseData inserUser(User user) {

        try {
            user.setUserId(UuidUtils.getUUId());
            Integer res = userMapper.insert(user);
            if (! (res > 0) ){
                return JsonData.fail("新增失败");
            }
        }catch (Exception e){

            return JsonData.fail("系统异常");
        }
        return JsonData.success();
    }


    @Override
    public ResponseData<Integer> updateUser(User user) {
        if (null == user){
            return JsonData.fail("用户信息为空！！");
        }
        if (null == user.getUserId()){
            return  JsonData.fail("主键信息为空！！！");
        }
        int update = this.userMapper.updateById(user);


        return JsonData.success(update);
    }

    @Override
    public ResponseData<Integer> delUser(String user_id) {
        if (null == user_id){
            return JsonData.fail("主键信息为空！！！");
        }
        int i = this.userMapper.deleteById(user_id);
        return JsonData.success(i);
    }

    @Override
    public ResponseData<List<User>> findUser(String user_name) {


        List<User> users = this.userMapper.selectList(new QueryWrapper<User>()
                .eq(null != user_name && "" != user_name, "user_name", user_name)
                .orderByDesc("user_name"));

//       List<User> users = this.userMapper.selectList(new QueryWrapper<User>().like("user_name",user_name));
       return JsonData.success(users);
    }
}
